import { defineConfig, loadEnv } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import vue from '@vitejs/plugin-vue'
import path from 'path'

const getViteEnv = (mode: any, target: any) => {
  return loadEnv(mode, process.cwd())[target]
}

// https://vitejs.dev/config/
export default ({ mode }) =>
  defineConfig({
    plugins: [
      vue(),
      createHtmlPlugin({
        inject: {
          data: {
            title: getViteEnv(mode, 'VITE_TITLE'),
            icon: getViteEnv(mode, 'VITE_ICON'),
          },
        },
      }),
    ],
    envPrefix: 'VITE_',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    server: {
      port: 10086,
      proxy: {
        '/dev': {
          target: 'http://localhost:8500',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev/, ''),
        },
      },
      cors: true,
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve(
              'src/style/less/base.less'
            )}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
  })
